package controllers
{
	import models.Project;
	import models.RESTResponse;
	import models.UMLClass;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;
		
	public class UMLClassManager
	{
		private var _current:UMLClass;
		[Bindable]
		private var _classes:ArrayCollection;
		
		public function UMLClassManager() {
			_current 	= null;
			_classes = new ArrayCollection
		}
		
		public function wakeUp() : void {
			updateList(Application.application.projectManager.currentProject, upadateListHandler);
		}
		
		public function updateList(project:Project, callback:Function) : void {
			HTTPController.GET('/classes/project/' + project.oid, null, function (response:RESTResponse) : void {
				upadateListHandler(response);
				callback(response);
			});
		}
		
		public function upadateListHandler(response:RESTResponse) : void {
			if(response.success()) {
				/* ATTENTION ! ne pas faire de new ... ça casse le data binding ! */
				_classes.removeAll();
				for each(var xml:XML in response.data()) {
					var c:UMLClass = new UMLClass();
					c.setXML(xml);
					_classes.addItem(c);
				}
			} else {
				UIErrorController.handleRESTErrorCode(response.errorCode());
			}
		}

		public function createClass(umlClass:UMLClass, callback:Function) : void {
			HTTPController.POST('/classes', umlClass.toXML(), function (response:RESTResponse) : void {
				if(response.success()) {
					callback(response);
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});
		}
		
		public function updateClass(umlClass:UMLClass, callback:Function) : void {
			HTTPController.PUT('/classes', umlClass.toXML(), function (response:RESTResponse) : void {
				if(response.success()) {
					callback(response);
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});	
		}
		
		public function getClassById(oid:int) : UMLClass {
			for each(var umlClass:UMLClass in _classes) {
				if(umlClass.oid == oid) {
					return umlClass;
				}
			}
			return null;
		}
		
		public function saveClass(projectId:int, abstractClass:Boolean, enumClass:Boolean, interfaceClass:Boolean, userClass:Boolean, name:String) : void {
			if(_current == null) {
				var umlClass:UMLClass = new UMLClass(projectId, name);
				umlClass.userClass = userClass;
				umlClass.abstractClass = abstractClass;
				umlClass.enumClass = enumClass;
				umlClass.interfaceClass = interfaceClass;
				createClass(umlClass, function (response:RESTResponse) : void {
					updateList(Application.application.projectManager.currentProject, function (response:RESTResponse) : void {
						upadateListHandler(response);
						Application.application.diagramManager.drawDiagram();
					});
				});
			} else {
				_current.projectId = projectId;
				_current.userClass = userClass;
				_current.abstractClass = abstractClass;
				_current.enumClass = enumClass;
				_current.interfaceClass = interfaceClass;
				_current.name = name;
				
				updateClass(_current, function (response:RESTResponse) : void {
					updateList(Application.application.projectManager.currentProject, function (response:RESTResponse) : void {
						upadateListHandler(response);
						Application.application.diagramManager.drawDiagram();
					});
				});
				_current = null;
			}
		}
		
		public function set currentClass(umlClass:UMLClass) : void {
			_current = umlClass;
		} 
		
		public function get currentClass() : UMLClass {
			return _current;
		}
		
		public function get classes() : ArrayCollection {
			return _classes;
		}
	}
}